# CMakeLists.txt

# For jpeg.lib



cmake_minimum_required(VERSION 2.4.4)
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)

if(UNIX AND NOT DEFINED CMAKE_BUILD_TYPE)
  if(CMAKE_MAJOR_VERSION EQUAL 2 AND CMAKE_MINOR_VERSION EQUAL 4)
    # workaround CMake 2.4.x bug
    set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
        "Choose the type of build, options are:
           None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used)
           Debug
           Release
           RelWithDebInfo
           MinSizeRel.")
  else()
    set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
        "Choose the type of build, options are:
           None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used)
           Debug
           Release
           RelWithDebInfo
           MinSizeRel.")
  endif()
endif()

project(jpeglib C)
enable_testing()

set(JPEGLIB_MAJOR 1)
set(JPEGLIB_MINOR 5)
set(JPEGLIB_RELEASE 12)
set(JPEGLIB_NAME libjpeg${JPEGLIB_MAJOR}${JPEGLIB_MINOR})
set(JPEGLIB_VERSION ${JPEGLIB_MAJOR}.${JPEGLIB_MINOR}.${JPEGLIB_RELEASE})

# needed packages
find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIR})

if(NOT WIN32)
  find_library(M_LIBRARY
    NAMES m
    PATHS /usr/lib /usr/local/lib
  )
  if(NOT M_LIBRARY)
    message(STATUS
      "math library 'libm' not found - floating point support disabled")
  endif()
else()
  # not needed on windows
  set(M_LIBRARY "")
endif()

# COMMAND LINE OPTIONS
if(DEFINED JPEG_SHARED)
  option(JPEG_SHARED "Build shared lib" ${JPEG_SHARED})
else()
  option(JPEG_SHARED "Build shared lib" ON)
endif()
if(DEFINED JPEG_STATIC)
  option(JPEG_STATIC "Build static lib" ${JPEG_STATIC})
else()
  option(JPEG_STATIC "Build static lib" ON)
endif()

option(JPEG_TESTS  "Build libjpeg tests" YES)

# Many more configuration options could be added here
option(JPEG_DEBUG         "Build with debug output" NO)
option(JPEGARG            "Disable ANSI-C prototypes" NO)

# SET LIBNAME
set(JPEG_LIB_NAME jpeg${JPEGLIB_MAJOR}${JPEGLIB_MINOR})

# to distinguish between debug and release lib
set(CMAKE_DEBUG_POSTFIX "d")

# Use the prebuilt jpeglibconf.h file from the scripts folder
# TODO: fix this by building with awk; without this no cmake build can be
# configured directly (to do so indirectly use your local awk to build a
# jpeglibconf.h in the build directory.)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/jconfig.txt
               ${CMAKE_CURRENT_BINARY_DIR}/jconfig.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

# OUR SOURCES
set(libjpeg_public_hdrs
  jerror.h jmorecfg.h jpeglib.h
  cderror.h cdjpeg.h jdct.h jinclude.h jmemsys.h jpegint.h 
        jversion.h transupp.h
  ${CMAKE_CURRENT_BINARY_DIR}/jconfig.h
)
set(libjpeg_sources
  ${libjpeg_public_hdrs}
  jaricom.c jcapimin.c jcapistd.c jcarith.c jccoefct.c jccolor.c 
        jcdctmgr.c jchuff.c jcinit.c jcmainct.c jcmarker.c jcmaster.c 
        jcomapi.c jcparam.c jcprepct.c jcsample.c jctrans.c jdapimin.c 
        jdapistd.c jdarith.c jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c 
        jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c 
        jdmerge.c jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c 
        jfdctfst.c jfdctint.c jidctflt.c jidctfst.c jidctint.c jquant1.c 
        jquant2.c jutils.c jmemmgr.c jmemnobs.c
)

# SOME NEEDED DEFINITIONS

add_definitions(-DJPEG_CONFIGURE_LIBJPEG)

if(MSVC)
  add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
endif(MSVC)

if(JPEG_DEBUG)
  add_definitions(-DJPEG_DEBUG)
endif()

# NOW BUILD OUR TARGET
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIR})

if(JPEG_SHARED)
  add_library(${JPEG_LIB_NAME} SHARED ${libjpeg_sources})
  if(MSVC)
    # msvc does not append 'lib' - do it here to have consistent name
    set_target_properties(${JPEG_LIB_NAME} PROPERTIES PREFIX "lib")
    set_target_properties(${JPEG_LIB_NAME} PROPERTIES IMPORT_PREFIX "lib")
  endif()
  target_link_libraries(${JPEG_LIB_NAME} ${ZLIB_LIBRARY} ${M_LIBRARY})
endif()

if(JPEG_STATIC)
# does not work without changing name
  set(JPEG_LIB_NAME_STATIC ${JPEG_LIB_NAME}_static)
  add_library(${JPEG_LIB_NAME_STATIC} STATIC ${libjpeg_sources})
  if(MSVC)
    # msvc does not append 'lib' - do it here to have consistent name
    set_target_properties(${JPEG_LIB_NAME_STATIC} PROPERTIES PREFIX "lib")
  endif()
  target_link_libraries(${JPEG_LIB_NAME_STATIC} ${ZLIB_LIBRARY} ${M_LIBRARY})
endif()

if(JPEG_SHARED AND WIN32)
  set_target_properties(${JPEG_LIB_NAME} PROPERTIES DEFINE_SYMBOL JPEG_BUILD_DLL)
endif()

# Ensure the CMAKE_LIBRARY_OUTPUT_DIRECTORY is set
IF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
ENDIF(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)

# Set a variable with CMake code which:
# Creates a symlink from src to dest (if possible) or alternatively
# copies if different.
macro(CREATE_SYMLINK SRC_FILE DEST_FILE)
  FILE(REMOVE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
  if(WIN32 AND NOT CYGWIN AND NOT MSYS)
    ADD_CUSTOM_COMMAND(
        OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}   ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
        COMMAND ${CMAKE_COMMAND} -E copy        ${CMAKE_CURRENT_BINARY_DIR}/${SRC_FILE} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}
        COMMAND ${CMAKE_COMMAND} -E copy        ${CMAKE_CURRENT_BINARY_DIR}/${SRC_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
        DEPENDS ${JPEG_LIB_NAME} ${JPEG_LIB_NAME_STATIC}
        )
    ADD_CUSTOM_TARGET(${DEST_FILE}_COPY ALL DEPENDS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
  else(WIN32 AND NOT CYGWIN AND NOT MSYS)
    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${SRC_FILE} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${SRC_FILE} ${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  endif(WIN32 AND NOT CYGWIN AND NOT MSYS)
endmacro()

# libjpeg is a library so default to 'lib'
if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
  set(CMAKE_INSTALL_LIBDIR lib)
endif(NOT DEFINED CMAKE_INSTALL_LIBDIR)

# CREATE PKGCONFIG FILES
# we use the same files like ./configure, so we have to set its vars
# Only do this on Windows for Cygwin - the files don't make much sense outside
# a UNIX look alike
if(NOT WIN32 OR CYGWIN OR MINGW) 
  set(prefix      ${CMAKE_INSTALL_PREFIX})
  set(exec_prefix ${CMAKE_INSTALL_PREFIX})
  set(libdir      ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
  set(includedir  ${CMAKE_INSTALL_PREFIX}/include)
  set(LIBS        "-lz -lm")
  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libjpeg.pc.in
    ${CMAKE_CURRENT_BINARY_DIR}/${JPEGLIB_NAME}.pc @ONLY)
  CREATE_SYMLINK(${JPEGLIB_NAME}.pc libjpeg.pc)

  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libjpeg-config.in
    ${CMAKE_CURRENT_BINARY_DIR}/${JPEGLIB_NAME}-config @ONLY)
  CREATE_SYMLINK(${JPEGLIB_NAME}-config libjpeg-config)
endif(NOT WIN32 OR CYGWIN OR MINGW)

# SET UP LINKS
if(JPEG_SHARED)
  set_target_properties(${JPEG_LIB_NAME} PROPERTIES
#   VERSION 15.${JPEGLIB_RELEASE}.1.5.12
    VERSION 15.${JPEGLIB_RELEASE}.0
    SOVERSION 15
    CLEAN_DIRECT_OUTPUT 1)
endif()
if(JPEG_STATIC)
  # MSVC doesn't use a different file extension for shared vs. static
  # libs.  We are able to change OUTPUT_NAME to remove the _static
  # for all other platforms.
  if(NOT MSVC)
    set_target_properties(${JPEG_LIB_NAME_STATIC} PROPERTIES
      OUTPUT_NAME ${JPEG_LIB_NAME}
      CLEAN_DIRECT_OUTPUT 1)
  endif()
endif()

# If CMake > 2.4.x, we set a variable used below to export
# targets to an export file.
# TODO: Use VERSION_GREATER after our cmake_minimum_required >= 2.6.2
if(CMAKE_MAJOR_VERSION GREATER 1 AND CMAKE_MINOR_VERSION GREATER 4)
  set(JPEG_EXPORT_RULE EXPORT libjpeg)
elseif(CMAKE_MAJOR_VERSION GREATER 2) # future proof
  set(JPEG_EXPORT_RULE EXPORT libjpeg)
endif()

# INSTALL
if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
  if(JPEG_SHARED)
    install(TARGETS ${JPEG_LIB_NAME}
        ${JPEG_EXPORT_RULE}
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

    # Create a symlink for libjpeg.dll.a => libjpeg15.dll.a on Cygwin
    if(CYGWIN OR MINGW)
       get_target_property(BUILD_TARGET_LOCATION ${JPEG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
       get_filename_component(BUILD_TARGET_FILE ${BUILD_TARGET_LOCATION} NAME)
       CREATE_SYMLINK(${BUILD_TARGET_FILE} libjpeg${CMAKE_IMPORT_LIBRARY_SUFFIX})
       install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libjpeg${CMAKE_IMPORT_LIBRARY_SUFFIX}
         DESTINATION ${CMAKE_INSTALL_LIBDIR})
    endif(CYGWIN OR MINGW)

    if(NOT WIN32)
      get_target_property(BUILD_TARGET_LOCATION ${JPEG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
      get_filename_component(BUILD_TARGET_FILE ${BUILD_TARGET_LOCATION} NAME)
      CREATE_SYMLINK(${BUILD_TARGET_FILE} libjpeg${CMAKE_SHARED_LIBRARY_SUFFIX})
      install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libjpeg${CMAKE_SHARED_LIBRARY_SUFFIX}
         DESTINATION ${CMAKE_INSTALL_LIBDIR})
    endif(NOT WIN32)
  endif(JPEG_SHARED)

  if(JPEG_STATIC)
    install(TARGETS ${JPEG_LIB_NAME_STATIC}
        ${JPEG_EXPORT_RULE}
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
    if(NOT WIN32 OR CYGWIN OR MINGW)
      get_target_property(BUILD_TARGET_LOCATION ${JPEG_LIB_NAME_STATIC} LOCATION_${CMAKE_BUILD_TYPE})
      get_filename_component(BUILD_TARGET_FILE ${BUILD_TARGET_LOCATION} NAME)
      CREATE_SYMLINK(${BUILD_TARGET_FILE} libjpeg${CMAKE_STATIC_LIBRARY_SUFFIX})
      install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libjpeg${CMAKE_STATIC_LIBRARY_SUFFIX}
         DESTINATION ${CMAKE_INSTALL_LIBDIR})
    endif(NOT WIN32 OR CYGWIN OR MINGW)
 endif()
endif()

if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
  install(FILES ${libjpeg_public_hdrs}   DESTINATION include)
  install(FILES ${libjpeg_public_hdrs}   DESTINATION include/${JPEGLIB_NAME})
endif()
if(NOT SKIP_INSTALL_EXECUTABLES AND NOT SKIP_INSTALL_ALL )
  if(NOT WIN32 OR CYGWIN OR MINGW)
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libjpeg-config DESTINATION bin)
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${JPEGLIB_NAME}-config
            DESTINATION bin)
  endif(NOT WIN32 OR CYGWIN OR MINGW)
endif()

if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
  # Install man pages
  if(NOT JPEG_MAN_DIR)
    set(JPEG_MAN_DIR "share/man")
  endif()
  install(FILES libjpeg.3 libjpegpf.3      DESTINATION ${JPEG_MAN_DIR}/man3)
  install(FILES jpeg.5                    DESTINATION ${JPEG_MAN_DIR}/man5)
  # Install pkg-config files
  if(NOT WIN32 OR CYGWIN OR MINGW)
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libjpeg.pc
            DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libjpeg-config
            DESTINATION bin)
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${JPEGLIB_NAME}.pc
            DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${JPEGLIB_NAME}-config
            DESTINATION bin)
  endif(NOT WIN32 OR CYGWIN OR MINGW)
endif()

# On versions of CMake that support it, create an export file CMake
# users can include() to import our targets
if(JPEG_EXPORT_RULE AND NOT SKIP_INSTALL_EXPORT AND NOT SKIP_INSTALL_ALL )
  install(EXPORT libjpeg DESTINATION lib/libjpeg FILE lib${JPEG_LIB_NAME}.cmake)
endif()

# what's with libjpeg-$VER%.txt and all the extra files?

# UNINSTALL
# do we need this?

# DIST
# do we need this?

